/**
 *Project: jh-framework
 *File: cn.xkshow.lang.StringHelper.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-3-27        xkshow.cn         create
 ****************************************************************
 *note: 
 * 1.计算一个字符串中出现次数最多的字符
 ***************************************************************/ 
package cn.xkshow.lang;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class StringHelper {
	
	/*
	 * isNotNull
	 */
	public static boolean isNotNull(String value) {
		return ((value != null) && (!("".equals(value.trim()))));
	}

	public static boolean isNotNull(Object value) {
		if (value != null) {
			return ((!(value instanceof String)) || (!(""
					.equals(((String) value).trim()))));
		}

		return false;
	}

	public static boolean isNotNull(String[] value) {
		return ((value != null) && (value.length != 0));
	}
	
	/*
	 * 字符移位
	 */	
	public static String reverse(String coderstr){//颠倒移位
		return new StringBuffer(coderstr).reverse().toString();
	}
		
	public static String swapOddEven(String coderstr){// 奇偶互移
		char[] ary = coderstr.toCharArray();
		char c;
		for (int i = 1; i < ary.length; i+=2) {			
			c = ary[i-1];			
			ary[i-1] = ary[i];
			ary[i] = c;
		}
		return String.valueOf(ary);
	}
		
	public static String swapComplex(String coderstr){// 复合移位
		char[] ary = new StringBuffer(coderstr).reverse().toString().toCharArray();
		char c;
		for (int i = 2; i < ary.length; i+=3) {
			c = ary[i-2];
			if(i%2 == 0){				
				ary[i-2] = ary[i];
				ary[i] = c;
			}else{
				ary[i-2] = ary[i-1];
				ary[i-1] = c;
			}			
		}
		return new StringBuffer(String.valueOf(ary)).reverse().toString();
	}
	
	/*
	 * 
	 */
	public static String escapeHTMLTags(String input) {
		if ((input == null) || (input.length() == 0)) {
			return input;
		}

		StringBuffer buf = new StringBuffer(input.length() + 6);
		char ch = ' ';
		for (int i = 0; i < input.length(); ++i) {
			ch = input.charAt(i);
			if (ch == '<') {
				buf.append("&lt;");
			} else if (ch == '>') {
				buf.append("&gt;");
			} else if (ch == '"') {
				buf.append("&quot;");
			} else if (ch == '\'') {
				buf.append("&#39;");
			} else {
				buf.append(ch);
			}
		}
		return buf.toString();
	}

	
	
	public static String array2String(String[] args, String arg) {
		StringBuffer sb = new StringBuffer();

		if ((args == null) || (args.length <= 0)) {
			return "";
		}

		for (int i = 0; i < args.length; ++i) {
			if ((args[i] == null) || (args[i].trim().equals(""))) {
				sb.append(arg);
			} else {
				sb.append(arg + args[i]);
			}

		}

		if (sb.length() > 0) {
			return sb.substring(arg.length());
		}

		return sb.toString();
	}

	public static String getQueryString(HttpServletRequest request, String key) {
		return getQueryString(request, key, "");
	}

	public static String getQueryString(HttpServletRequest request, String key,
			String defaultValue) {
		String rtnValue = request.getParameter(key);
		if ((rtnValue == null) || (rtnValue.trim().length() == 0)) {
			rtnValue = defaultValue;
		}

		rtnValue = toGBK(rtnValue);

		return rtnValue.trim();
	}

	public static String toGBK(String source)
  {
    if (source == null)
    {
      return null;
    }

    if (source.trim().equals(""))
    {
      return source;
    }

    String retStr = source;
    try
    {
      byte[] b = source.getBytes("ISO8859_1");

      for (int i = 0; i < b.length; ++i)
      {
        byte b1 = b[i];
        if (b1 == 63)
        {
          break ;
        }
        if (b1 > 0)
        {
          continue;
        }
        if (b1 < 0)
        {
          retStr = new String(b, "GBK");
          break ;
        }
      }
    }
    catch (UnsupportedEncodingException e)
    {
      e.printStackTrace();
    }

    return retStr;
  }

	public static String[] toGBK(String[] source) {
		String[] ret = new String[source.length];
		for (int i = 0; i < source.length; ++i) {
			ret[i] = toGBK(source[i]);
		}

		return ret;
	}

	public static String truncateString(String source, int maxLength,
			String substitute) {
		if (isNotNull(source)) {
			char[] array = source.toCharArray();
			int length = 0;
			for (int i = 0; i < array.length; ++i) {
				char c = array[i];
				if ((c <= 0) || (c >= '~')) {
					length += 2;
				} else {
					++length;
				}
			}

			if ((length > maxLength) && (source.length() > maxLength)) {
				return source.substring(0, maxLength) + substitute;
			}
		}

		return source;
	}

	public static String toPlainText(String input) {
		if (input == null) {
			return "";
		}

		input = input.replaceAll("</?[^>]+>", "");

		return input;
	}

	public static Integer[] convert2IntegerArray(String[] strValues)
			throws ClassCastException {
		if ((strValues == null) || (strValues.length == 0)) {
			return null;
		}
		Integer[] integerValues = new Integer[strValues.length];
		for (int i = 0; i < strValues.length; ++i) {
			integerValues[i] = Integer.valueOf(strValues[i]);
		}

		return integerValues;
	}

	public static Long[] convert2LongArray(String[] strValues)
			throws ClassCastException {
		if ((strValues == null) || (strValues.length == 0)) {
			return null;
		}
		Long[] longValues = new Long[strValues.length];
		for (int i = 0; i < strValues.length; ++i) {
			longValues[i] = Long.valueOf(strValues[i]);
		}

		return longValues;
	}

	public static String toHtmlText(String input) {
		String ret = escapeHTMLTags(input);

		ret = replace(ret, "\r\n", "<br>");
		ret = replace(ret, "\n", "<br>");
		ret = replace(ret, "\r", "<br>");

		return ret;
	}

	public static String textToHtml(String input) {
		return toHtmlText(input);
	}

	/*
	 * 计算一个字符串中出现次数最多的字符
	 */
	public static int countCharInStr(char ch, String txt, int icount){
		int i = txt.indexOf(ch);
		if(i == -1){
			return icount;
		}else{
			icount++;
			return countCharInStr(ch, txt.substring(++i), icount);
		}
	}
	
	public static void countMaxCharInStr(String txt){
        char[] content = txt.toCharArray();   
        Map<Character, Integer> map = new HashMap<Character, Integer>();   
        for(int i = 0; i < content.length; i ++){
        	map.put(content[i], countCharInStr(content[i], txt, 0));
        }
                       
        int maxValue = map.get(content[0]);   
        char c = ' ';   
        for(int j = 0; j < content.length; j ++){   
            if(maxValue < map.get(content[j])){   
                maxValue = map.get(content[j]);   
                c = content[j];   
            }   
        }
        System.out.println("Now the highest number of characters:" + c + " Count:" + maxValue);
	}
	
	/*
	 * private
	 */
	private static String replace(String line, String oldString,
			String newString) {
		if (line == null) {
			return null;
		}
		int i = 0;
		if ((i = line.indexOf(oldString, i)) >= 0) {
			char[] line2 = line.toCharArray();
			char[] newString2 = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j = i;
			while ((i = line.indexOf(oldString, i)) > 0) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
				j = i;
			}
			buf.append(line2, j, line2.length - j);
			return buf.toString();
		}

		return line;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		countMaxCharInStr("Hello World");
	}

}
